<?php

class General {

    public static function getIp() {
        if (!empty($_SERVER['HTTP_CLIENT_IP']))
            return $_SERVER['HTTP_CLIENT_IP'];
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
        return $_SERVER['REMOTE_ADDR'];
    }

    public static function slugify($string, $replacement = '-') {
        $aux = preg_quote($replacement, '/');
        $map = array(
            '/à|á|ã|â/' => 'a',
            '/è|é|ê|ẽ|ë/' => 'e',
            '/ì|í|î/' => 'i',
            '/ò|ó|ô|õ|ø/' => 'o',
            '/ù|ú|ũ|û/' => 'u',
            '/ç/' => 'c',
            '/ñ/' => 'n',
            '/ä|æ/' => 'ae',
            '/ö/' => 'oe',
            '/ü/' => 'ue',
            '/Ä/' => 'Ae',
            '/Ü/' => 'Ue',
            '/Ö/' => 'Oe',
            '/ß/' => 'ss',
            '/[^\w\s]/' => ' ',
            '/\\s+/' => $replacement
        );
        return strtolower(preg_replace(array_keys($map), array_values($map), $string));
    }

    function miScapeURL($text) {
        $text = strtolower($text);
        $a = array('á', 'é', 'í', 'ó', 'ú', 'ñ', ' ', '&aacute;', '&eacute;', '&iacute;', '&oacute;', '&uacute;', '&ntilde;', '&');
        $b = array('a', 'e', 'i', 'o', 'u', 'n', '-', 'a', 'e', 'i', 'o', 'u', 'n', 'y');
        $text = str_replace($a, $b, $text);
        $text = preg_replace('#[^-a-zA-Z0-9_ ]#', '', $text);
        $text = trim($text);
        return $text;
    }

    public static function cortar($texto, $tam = 50, $let = false) {
        if (strlen($texto) > $tam) {
            $texto = substr($texto, 0, $tam);
            $texto = explode(" ", $texto);
            $num = count($texto) - 1;
            $texto = array_splice($texto, 0, $num);
            $texto = implode(" ", $texto) . "...";
        }
        return $texto;
    }

    public function leer_contenido_completo($url) {
        $fichero_url = fopen($url, "r");
        $texto = "";
        while ($trozo = fgets($fichero_url, 1024)) {
            $texto .= $trozo;
        }
        return $texto;
    }

}

?>
